package com.stu.vertx.hello.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Arrays;

/**
 * 自定义realm，编写认证和授权方法
 */
public class MyRealm extends AuthorizingRealm {


    /**
     * realm名称
     * @return
     */
    @Override
    public String getName() {
        return "myRealm";
    }

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        String password = new String(token.getPassword());

        // 模拟数据库中的密码
        String dbPassword="12333";

        System.out.println("您输入的用户名: " + username);

        System.out.println("您输入的密码: " + password);

        // SimpleAuthenticationInfo会自动拿我们传递的(数据库中的)与token中的(前台传递的)用户名和密码进行对比
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, dbPassword, getName());

        return info;
    }
}
